
var app = getApp()
var util = require('../../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 导航栏标题
    navigateTitle: "",
    // 请求路径
    requestUrl: "",
    // 电影列表
    movies : {},
    // 总数
    totalCount : 0,
    isEmpty: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var category = options.category;
    this.data.navigateTitle = category;

    // 设置导航栏标题
    wx.setNavigationBarTitle({
      title: this.data.navigateTitle
    })

    // 判断requestUrl
    var dataUrl = "";
    switch (category) {
      case "正在热映":
        dataUrl = "/v2/movie/in_theaters";
        break;
      case "即将上映":
        dataUrl = "/v2/movie/coming_soon";
        break;
      case "豆瓣Top250":
        dataUrl = "/v2/movie/top250";
        break;
    }
    dataUrl = app.globalData.doubanBase + dataUrl;
    this.data.requestUrl = dataUrl;
    // 发起请求
    wx.startPullDownRefresh()
    util.http(this.data.requestUrl, this.processDoubanData)
  },

  // 处理豆瓣数据
  processDoubanData:function(options) {
    var movies = [];
    var moviesDouban = options;
    for (var idx in moviesDouban.subjects) {
      var subject = moviesDouban.subjects[idx];
      // 处理标题
      var title = subject.title;
      if (title.length >= 6) {
        title = title.substring(0, 6) + "...";
      }
      // 处理数据, 并添加到数组
      var temp = {
        stars: util.convertToStarsArray(subject.rating.stars),
        title: title,
        average: subject.rating.average,
        coverageUrl: subject.images.large,
        movieId: subject.id
      }
      movies.push(temp)
    }

    // 数据绑定
    var totalMovies = {};
    //如果要绑定新加载的数据，那么需要同旧有的数据合并在一起
    if (!this.data.isEmpty) {
      totalMovies = this.data.movies.concat(movies);
    }
    else {
      totalMovies = movies;
      this.data.isEmpty = false;
    }
    this.setData({
      movies : totalMovies
    });

    this.data.totalCount += 20;
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  }, 

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh : function() {
    console.log("下拉刷新");
    var refreshUrl = this.data.requestUrl +
      "?star=0&count=20";
    // 清空数据
    this.data.movies = {};
    this.data.isEmpty = true;
    this.data.totalCount = 0;
    // 发起请求
    util.http(refreshUrl, this.processDoubanData);
    wx.showNavigationBarLoading();
  },

  onReachBottom: function (event) {
    console.log("上拉刷新");
    var nextUrl = this.data.requestUrl +
      "?start=" + this.data.totalCount + "&count=20";
    util.http(nextUrl, this.processDoubanData)
    wx.showNavigationBarLoading()
  },

  // onScrollLower: function() {
  //   var nextUrl = this.data.requestUrl +
  //     "?start=" + this.data.totalCount + "&count=20";
  //   util.http(nextUrl, this.processDoubanData)
  //   wx.showNavigationBarLoading()
  // }

  onMovieTap:function (event) {
    var movieId = event.currentTarget.dataset.movieid;
    wx.navigateTo({
      url: "../movie-detail/movie-detail?id=" + movieId
    })
  }
})